<?php
namespace app\controllers;

use app\controllers\BaseController;
use app\models\User;

class AdminController extends BaseController{
    public function actionAdd(){
        $post = \Yii::$app->request->post('User');
        if(strlen($post['user_admin'])<3){
            $result = ['result'=>0,'data'=>'登录用户名太短，请重新输入'];
            return json_encode($result);
        }
        $is_admin = User::find()->where('user_admin=:admin',[':admin' => $post['user_admin']])->count();
        if($is_admin){
            $result = ['result'=>0,"data"=>'已经存在登录用户名，请更换'];
            return json_encode($result);
        }
        if(strlen($post['user_pass'])<5){
            $result = ['result'=>0,'data'=>'登录密码太短'];
            return json_encode($result);
        }
        if($post['user_pass']!=$post['user_pass2']){
            $result = ['result'=>0,'data'=>'两次输入密码不一样'];
            return json_encode($result);
        }
        if(empty($post['user_name'])){
            $result = ['result'=>0,'data'=>'请输入姓名'];
            return json_encode($result);
        }
        $model = new User();
        $model->user_admin = $post['user_admin'];
        $model->user_pass = \Yii::$app->security->generatePasswordHash($post['user_pass']);
        $model->user_name = $post['user_name'];
        $model->authKey = \Yii::$app->security->generateRandomString();
        $model->addtime = time();
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionList(){
        $list = User::find()->orderBy('addtime DESC')->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }

    public function actionInfo(){
        $id = \Yii::$app->request->post('id');
        $model = User::find()->select(['id','user_admin','user_name'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }

    public function actionSaveinfo(){
        $post = \Yii::$app->request->post('form');
        $model = User::find()->where('id=:id',[':id' => $post['id']])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->user_name = $post['user_name'];
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionPass(){
        $id = \Yii::$app->request->post('id');
        $model = User::find()->select(['id','user_admin'])->where('id=:id',[':id' => $id])->asArray()->one();
        if(empty($model)){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }

    public function actionSavepass(){
        $post = \Yii::$app->request->post('form');
        $user_admin = $post['user_admin'];
        $user_pass_old = $post['user_pass_old'];
        $user_pass = $post['user_pass'];
        $user_pass2 = $post['user_pass2'];
        $model = User::find()->where('user_admin=:name',[':name' => $user_admin])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        if(empty($user_pass_old)){
            $result = ['result'=>0,'data'=>'请输入旧密码'];
            return json_encode($result);
        }
        if(empty($user_pass)){
            $result = ['result'=>0,'data'=>'请输入新密码'];
            return json_encode($result);
        }
        if(strlen($user_pass)<5){
            $result = ['result'=>0,'data'=>'新密码太短'];
            return json_encode($result);
        }
        if(!\Yii::$app->security->validatePassword($user_pass_old,$model->user_pass)){
            $result = ['result'=>0,'data'=>'旧密码不正确，请重新输入'];
            return json_encode($result);
        }
        if($user_pass!=$user_pass2){
            $result = ['result'=>0,'data'=>'两次输入密码不正确'];
            return json_encode($result);
        }
        $model->user_pass = \Yii::$app->security->generatePasswordHash($user_pass);
        $model->addtime = time();
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

    public function actionDel(){
        $id  = \Yii::$app->request->post('id');
        $model = User::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result);
    }

}
